﻿using ML.Entity;
using ML.IBusiness;
using ML.IRepository;

namespace ML.Business
{
    /// <summary>
    /// 邀请计数业务实现
    /// </summary>
    public class InvitationProfitBusiness : IInvitationProfitBusiness
    {
        private readonly IInvitationProfitRepository invitationProfitRepository;

        public InvitationProfitBusiness(IInvitationProfitRepository _invitationProfitRepository)
        {
            this.invitationProfitRepository = _invitationProfitRepository;
        }

        public BaseResult<bool> UpdataInvitationCountByUserId(int invitationCount, string userId)
        {
            var isTrue = invitationProfitRepository.UpdataSuccessCountCountByUserId(invitationCount, userId);
            return new BaseResult<bool>(isTrue);
        }

        public BaseResult<bool> UpdataSuccessCountCountByUserId(int successCount, string userId)
        {
            var isTrue = invitationProfitRepository.UpdataSuccessCountCountByUserId(successCount, userId);
            return new BaseResult<bool>(isTrue);
        }

        public BaseResult<InvitationProfitEntity> GetByUserId(string userId)
        {
            var data = invitationProfitRepository.GetByUserId(userId);
            return new BaseResult<InvitationProfitEntity>(200, data);
        }

        public BaseResult<bool> Add(InvitationProfitEntity entity)
        {
            int count = 0;
            var r = invitationProfitRepository.GetByUserId(entity.UserId);
            if (r != null)
            {
                count = r.InvitationCount + 1;
                var isTrue = invitationProfitRepository.UpdataInvitationCountByUserId(count, entity.UserId);
                return new BaseResult<bool>(isTrue);
            }
            else
            {
                entity.InvitationCount = 1;
                entity.SuccessCount = 0;
                var isTrue = invitationProfitRepository.Add(entity);
                return new BaseResult<bool>(isTrue);
            }
        }
    }
}